package com.wxtt.android.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fast.dev.frame.glide.GlideLoader;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.view.SwitchButton;
import com.wxtt.android.R;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.config.ApiConfig;
import com.wxtt.android.config.ConstantConfig;
import com.wxtt.android.config.GlideConfig;
import com.wxtt.android.config.SpConfig;
import com.wxtt.android.dialog.TipDialog;
import com.wxtt.android.view.HandlerHelper;

import cn.bmob.push.BmobPush;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.BmobUpdateListener;
import cn.bmob.v3.update.UpdateResponse;
import cn.bmob.v3.update.UpdateStatus;

/**
 * 说明：设置界面
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/6 14:59
 * <p/>
 * 版本：verson 1.0
 */
public class SettingActivity extends CommonActivity{

    //推送设置
    @Bind(id = R.id.rl_push,click = true)
    private RelativeLayout rl_push;
    //升级设置
    @Bind(id = R.id.rl_update,click = true)
    private RelativeLayout rl_update;
    //关于
    @Bind(id = R.id.ll_about,click = true)
    private LinearLayout ll_about;
    @Bind(id = R.id.sb_update)
    private SwitchButton sb_update;
    @Bind(id = R.id.sb_push)
    private SwitchButton sb_push;
    @Bind(id = R.id.tv_clean_cache,click = true)
    private TextView tv_clean_cache;
    @Bind(id = R.id.tv_update,click = true)
    private TextView tv_update;

    private TipDialog tipDialog;

    @Override
    public int setRootView() {
        return R.layout.activity_setting;
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }

    @Override
    public int setToolbarTitle() {
        return R.string.navigation_item_system_setting;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onInit(Bundle bundle) {
        initWidgetState();
        sb_push.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SpConfig.setPushState(true);
                } else {
                    SpConfig.setPushState(false);
                }
            }
        });
        sb_update.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SpConfig.setWifiUpdate(true);
                } else {
                    SpConfig.setWifiUpdate(false);
                }
            }
        });
    }

    /**
     * 说明：初始化开关状态
     */
    private void initWidgetState(){
        sb_push.setCheckedImmediately(SpConfig.isPushState());
        sb_update.setCheckedImmediately(SpConfig.isWifiUpdate());
    }

    @Override
    public void clickView(View v) {
        boolean flag;
        switch (v.getId()){
            case R.id.tv_update:
                showLoading(getString(R.string.setting_update_checking));
                HandlerHelper.postDelayMain(3000, new HandlerHelper.MainRunListener() {
                    @Override
                    public void run() {
                        ApiConfig.checkUpdate(new BmobUpdateListener() {
                            @Override
                            public void onUpdateReturned(int i, UpdateResponse updateResponse) {
                                dismissLoading();
                                if (i == UpdateStatus.Yes) {
                                } else if (i == UpdateStatus.No) {
                                    shortToast("您已经是最新版本");
                                } else {
                                    shortToast(updateResponse.getException().getMessage());
                                }
                            }
                        });
                        ApiConfig.checkUpdate(SettingActivity.this);
                    }
                });
                break;
            case R.id.rl_push:
                flag = sb_push.isChecked();
                sb_push.setChecked(!flag);
                break;
            case R.id.rl_update:
                flag = sb_update.isChecked();
                sb_update.setChecked(!flag);
                break;
            case R.id.ll_about:
                showActivity(this, AboutActivity.class);
                break;
            case R.id.tv_clean_cache:
                if (tipDialog == null) {
                    tipDialog = new TipDialog(this);
                    tipDialog.setContent(getString(R.string.setting_update_clean_tips));
                    tipDialog.setSureText("清理").setCancelText("取消")
                            .setSureTextColor(R.color.toolbarColor)
                            .setCancelTextColor(R.color.toolbarColor)
                            .setTip("").setClickListener(new TipDialog.TipClickListener() {
                        @Override
                        public boolean onSure() {
                            GlideLoader.clean(GlideConfig.CACHE);
                            shortToast("清理完成");
                            return false;
                        }

                        @Override
                        public boolean onCancel() {
                            return false;
                        }
                    });
                }
                tipDialog.show();
                break;
        }
    }
}
